package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.oms.entity.YytOmsOrder;
import com.ruoyi.common.oms.service.YytOmsOrderService;
import com.ruoyi.common.pms.entity.YytMaaSubscribeStore;
import com.ruoyi.common.pms.service.YytMaaSubscribeStoreService;
import com.ruoyi.utils.ResponseResult;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.pms.entity.YytMaaCraftsman;
import com.ruoyi.common.pms.service.YytMaaCraftsmanService;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
    * 服务手艺人表 前端控制器
    * </p>
*
* @author MrPing
* @since 2022-03-14
*/
@RestController
@RequestMapping("/craftsman")
public class YytMaaCraftsmanController {

    @Autowired
    private YytMaaCraftsmanService yytMaaCraftsmanService;

    @Autowired
    private YytOmsOrderService yytOmsOrderService;

    @Autowired
    private YytMaaSubscribeStoreService yytMaaSubscribeStoreService;


    /**
     * 获取当前店铺所有技师
     * @return
     */
    @GetMapping("/getCraftsmanDataAll")
    public ResponseResult getCraftsmanDataAll(@RequestParam("storeId") String storeId){
        QueryWrapper<YytMaaCraftsman> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);

        return ResponseResult.success(yytMaaCraftsmanService.list(wrapper));

    }

    /**
     * 根id获取手艺人数据
     */
    @GetMapping("/getServiceName")
    public ResponseResult getServiceName(@RequestParam("id") String id){

        return ResponseResult.success(yytMaaCraftsmanService.getById(id));
    }

    /**
     * 根据微信openid获取用户是否存在
     * @param wechatId
     * @return
     */
    @GetMapping("/getWeChatInfo")
    public ResponseResult getWeChatInfo(@RequestParam("wechatId") String wechatId, @RequestParam("orderId") String orderId){
        YytOmsOrder order = yytOmsOrderService.getById(orderId);
        QueryWrapper<YytMaaCraftsman> wrapper = new QueryWrapper<>();
        wrapper.eq("wxid", wechatId).eq("store_id", order.getShopId());
        YytMaaCraftsman one = yytMaaCraftsmanService.getOne(wrapper);
        if(one == null){
            return ResponseResult.success(new ArrayList<>());
        }
        QueryWrapper<YytMaaSubscribeStore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId).eq("service_user_id", one.getId());
        return ResponseResult.success(yytMaaSubscribeStoreService.list(queryWrapper));
    }

}
